﻿using Vowei.Web.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.Mvc;
using System.IO;
using Vowei.Data.Converters.Excel;
using System.Collections.Generic;
using Vowei.Core.Models;
using Vowei.Data;
using System.Linq;
using Vowei.Data.Models;
using NMock2;
using Autofac;
using Vowei.Web;
using Autofac.Integration.Web;
using Vowei.Web.Helpers;
using Vowei.Data.Helpers;
using Vowei.Core;
using System.Globalization;

namespace vowei.test
{
    [TestClass()]
    public class AccountControllerTest : TestBase
    {
        [TestMethod()]
        public void ConfirmImportTest()
        {
            var filename = Path.Combine(
                TestContext.TestDeploymentDir, @"ExcelUserConverterTest_ConvertFromRealScenarioTest.xls");
            IEnumerable<ResourceAssignmentDetail> rads = null;
            Guid pid = Guid.Empty;

            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                var uce = new UserExcelConverter();
                var converter = new AssignmentExcelConverter(uce);
                rads = converter.ConvertFrom(stream);
            }

            var target = IocContainer.Resolve<AccountController>();
            using (var context = new VoweiContextImpl())
            {
                var project = CreateProjectHelper("ConfirmImportTest", new VoweiContext(context));
                pid = project.ID;
                context.Projects.Add(project);
                context.SaveChanges();
            }

            target.ImportUsersImpl(rads, pid);

            using (var context = new VoweiContextImpl())
            {
                var project = context.Projects.Include("ResourceAssignments").Single(p => p.ID == pid);
                var employee = context.Employees.First(e => e.Email == "yishi@vowei.com");
                Assert.IsTrue(project.ResourceAssignments.Any(a => a.ResourceId == employee.ID));
            }
        }

        [TestMethod]
        public void ImportNonEmailUsersTest()
        {
            var employees = new Employee[] 
            {
                new Employee() { UserName = @"vowei\administrator" },
                new Employee() { UserName = @"vowei\testuser" }
            };

            EntityHelpers.FixUserEmails(employees);
            var rads = new List<ResourceAssignmentDetail>(employees.Length);
            foreach (var e in employees)
            { 
                rads.Add(new ResourceAssignmentDetail
                {
                    Assignment = new ResourceAssignment { ResourceId = e.ID },
                    Employee = e
                });
            }

            var target = new AccountController(VoweiContext, Setting.DefaultFormUser);
            Guid pid = Guid.Empty;
            using (var context = new VoweiContextImpl())
            {
                var project = CreateProjectHelper("ImportNonEmailUsersTest");
                pid = project.ID;
                context.Projects.Add(project);
                context.SaveChanges();
            }

            target.ImportUsersImpl(rads, pid);
            using (var context = new VoweiContextImpl())
            {
                var project = context.Projects.Include("ResourceAssignments").Single(p => p.ID == pid);
                Assert.AreEqual(2, project.ResourceAssignments.Count());
                foreach (Employee employee in employees)
                {
                    var id = employee.ID;
                    Assert.IsTrue(project.ResourceAssignments.Any(a => a.ResourceId == id));
                }
            }
        }

        [TestMethod()]
        public void ImportTeamMemberAndThenAssignmentTest()
        {
            var filename = Path.Combine(
                TestContext.TestDeploymentDir, @"ExcelUserConverterTest_ConvertFromRealScenarioTest.xls");
            IEnumerable<ResourceAssignmentDetail> rads = null;
            Guid pid = Guid.Empty;

            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                var uce = new UserExcelConverter();
                var converter = new AssignmentExcelConverter(uce);
                rads = converter.ConvertFrom(stream);
            }

            var target = IocContainer.Resolve<AccountController>();
            using (var context = new VoweiContextImpl())
            {
                var project = CreateProjectHelper("ImportTeamMemberAndThenAssignmentTest", new VoweiContext(context));
                pid = project.ID;
                context.Projects.Add(project);
                context.SaveChanges();
            }

            target.ImportUsersImpl(rads, pid);

            filename = Path.Combine(
                TestContext.TestDeploymentDir, @"ExcelUserConverterTest_ImportTeamMemberAndThenAssignmentTest.xls");            
            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                var uce = new UserExcelConverter();
                var converter = new AssignmentExcelConverter(uce);
                rads = converter.ConvertFrom(stream);
            }
            target.ImportUsersImpl(rads, pid);

            using (var context = new VoweiContextImpl())
            {
                var project = context.Projects.Include("ResourceAssignments").Single(p => p.ID == pid);
                var employee = context.Employees.Single(e => e.Email == "yishi@vowei.com");
                var rid = employee.ID;
                var actual = project.ResourceAssignments.Single(ra => ra.ProjectId == pid && ra.ResourceId == rid);
                Assert.AreEqual(10.0d, actual.CostPerHour);
            }
        }

        [TestMethod()]
        public void ImportTeamMemberAndThenAssignmentBugRegressionTest()
        {
            IEnumerable<ResourceAssignmentDetail> rads = null;
            Guid pid = Guid.Empty;

            var target = IocContainer.Resolve<AccountController>();
            using (var context = new VoweiContextImpl())
            {
                var project = CreateProjectHelper("ImportTeamMemberAndThenAssignmentTest");
                pid = project.ID;
                context.Projects.Add(project);
                context.SaveChanges();
            }

            var filename = Path.Combine(
                TestContext.TestDeploymentDir, @"vowei project team member.xls");
            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                var uce = new UserExcelConverter();
                var converter = new AssignmentExcelConverter(uce);
                rads = converter.ConvertFrom(stream);
            }
            target.ImportUsersImpl(rads, pid);

            using (var context = new VoweiContextImpl())
            {
                var project = context.Projects.Include("ResourceAssignments").Single(p => p.ID == pid);
                var employee = context.Employees.Single(e => e.Email == "admin@vowei.com");
                var rid = employee.ID;
                var actual = project.ResourceAssignments.Single(ra => ra.ProjectId == pid && ra.ResourceId == rid);
                Assert.AreEqual(13.0d, actual.CostPerHour);
            }
        }
    }
}
